package com.wannengbang.cloudleader.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.DeviceUnifyReceivedNewBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.widget.AppTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MachinesDetailsActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_total_num)
    TextView tvTotalNum;
    @BindView(R.id.tv_able_use_count)
    TextView tvAbleUseCount;
    @BindView(R.id.tv_used_count)
    TextView tvUsedCount;
    @BindView(R.id.tv_leave_count)
    TextView tvLeaveCount;
    @BindView(R.id.tv_disable)
    TextView tvDisable;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_look1)
    LinearLayout llLook1;
    @BindView(R.id.ll_look2)
    LinearLayout llLook2;
    @BindView(R.id.ll_look3)
    LinearLayout llLook3;
    @BindView(R.id.ll_look4)
    LinearLayout llLook4;
    @BindView(R.id.tv_enable_child_count)
    TextView tvEnableChildCount;
    @BindView(R.id.ll_look5)
    LinearLayout llLook5;
    @BindView(R.id.ll_look11)
    LinearLayout llLook11;
    @BindView(R.id.ll_look12)
    LinearLayout llLook12;
    private String type;
    private String child_agent_id;
    private String typeUnit = "";
    private String agent_name;
    private String is_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machines_details);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {
        type = getIntent().getStringExtra("type");
        child_agent_id = getIntent().getStringExtra("child_agent_id");
        agent_name = getIntent().getStringExtra("agent_name");
        is_send = getIntent().getStringExtra("is_send");
        String titlePrefix = "";
        if (!TextUtils.isEmpty(agent_name)) {
            titlePrefix = agent_name + "_";
            if (agent_name.length() > 6) {
                titlePrefix = agent_name.substring(0, 6) + "_";
            }
        } else {
            titlePrefix = "";
        }

        if ("1".equals(type)) {
            titleBar.setTitle(titlePrefix + "云小宝");
            llLook11.setVisibility(View.GONE);
        } else if ("2".equals(type)) {
            titleBar.setTitle(titlePrefix + "云商宝");
            llLook11.setVisibility(View.GONE);
        } else if ("3".equals(type)) {
            titleBar.setTitle(titlePrefix + "云小宝Vip版");
            llLook11.setVisibility(View.GONE);
        } else if ("4".equals(type)) {
            titleBar.setTitle(titlePrefix + "云小宝Plus版");
            llLook11.setVisibility(View.GONE);
            llLook12.setVisibility(View.VISIBLE);
        } else if ("5".equals(type)) {
            titleBar.setTitle(titlePrefix + "云小宝红包版");
            llLook11.setVisibility(View.GONE);
            llLook12.setVisibility(View.VISIBLE);
        } else if ("6".equals(type)) {
            titleBar.setTitle(titlePrefix + "赠送的云小宝红包版");
            llLook11.setVisibility(View.GONE);
            llLook12.setVisibility(View.VISIBLE);
        }

        if ("0".equals(is_send)) {
            tvTitle.setText("当前采购机具总数量(台)");
        } else {
            tvTitle.setText("当前兑换机具总数量(台)");
        }
    }

    private void initData() {
        new HomePageModelImpl().requestChildDeviceStatisticsByUse(is_send, child_agent_id, type, new DataCallBack<DeviceUnifyReceivedNewBean>() {
            @Override
            public void onSuccessful(DeviceUnifyReceivedNewBean bean) {
                tvAbleUseCount.setText(bean.getData().getChild_un_use() + typeUnit);
                tvUsedCount.setText(bean.getData().getChild_use() + typeUnit);
                tvLeaveCount.setText(bean.getData().getNot_child() + typeUnit);
                tvDisable.setText(bean.getData().getChild_disable() + typeUnit);
                tvEnableChildCount.setText(bean.getData().getEnable_child_count() + typeUnit);
                int totalNum = bean.getData().getChild_un_use() + bean.getData().getChild_use() + bean.getData().getNot_child() + bean.getData().getChild_disable();
                tvTotalNum.setText(totalNum + "");
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_look1, R.id.ll_look2, R.id.ll_look3, R.id.ll_look4, R.id.ll_look5, R.id.ll_look11, R.id.ll_look12})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_look1:
                //未激活
                Intent intent = new Intent(mActivity, MachinesListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", "0");
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                intent.putExtra("is_send", is_send);
                startActivity(intent);
                break;
            case R.id.ll_look2:
                //已激活
                intent = new Intent(mActivity, MachinesListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", "1");
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                intent.putExtra("is_send", is_send);
                startActivity(intent);
                break;
            case R.id.ll_look3:
                //已划拨
                intent = new Intent(mActivity, TransferAgentListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                intent.putExtra("is_send", is_send);
                startActivity(intent);
                break;
            case R.id.ll_look4:
                //已禁用
                intent = new Intent(mActivity, MachinesListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", "2");
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                intent.putExtra("is_send", is_send);
                startActivity(intent);
                break;
            case R.id.ll_look5:
                //已激活 本机和下级
                intent = new Intent(mActivity, MachinesActivateListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", "1");
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                intent.putExtra("is_send", is_send);
                startActivity(intent);
                break;
            case R.id.ll_look11:
                //申请设置记录
                intent = new Intent(mActivity, ApplyVipListActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_look12:
                //申请设置记录
                intent = new Intent(mActivity, ApplyPlusRateListActivity.class);
                startActivity(intent);
                break;
        }
    }
}
